package com.example.accessingdatamybatisplus;

import com.example.accessingdatamybatisplus.mapper.CustomerMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * @author caiwl
 * @date 2021/6/22 16:33
 */
@SpringBootApplication
@MapperScan("com.example.accessingdatamybatisplus.mapper")
public class AccessingDataMybatisPlusApplication {

    private static final Logger log = LoggerFactory.getLogger(AccessingDataMybatisPlusApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(AccessingDataMybatisPlusApplication.class, args);
    }

    @Bean
    public CommandLineRunner demo(CustomerMapper mapper) {
        return (args) -> {
            // save a few customers
            mapper.insert(new Customer("Jack", "Bauer"));
            mapper.insert(new Customer("Chloe", "O'Brian"));
            mapper.insert(new Customer("Kim", "Bauer"));
            mapper.insert(new Customer("David", "Palmer"));
            mapper.insert(new Customer("Michelle", "Dessler"));

            // fetch all customers
            log.info("Customers found with selectAll():");
            log.info("-------------------------------");
            mapper.selectList(null).forEach(System.out::println);
            log.info("");

            // fetch an individual customer by ID
            Customer customer = mapper.selectById(1L);
            log.info("Customer found with selectByPrimaryKey(1L):");
            log.info("--------------------------------");
            log.info(customer.toString());
            log.info("");

            // fetch customers by last name
            log.info("Customer found with selectByLastName('Bauer'):");
            log.info("--------------------------------------------");
            mapper.selectByLastName("Bauer").forEach(bauer -> log.info(bauer.toString()));
            // for (Customer bauer : mapper.findByLastName("Bauer")) {
            //  log.info(bauer.toString());
            // }
            log.info("");
        };
    }
}
